package com.tomjun.permission.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.tomjun.common.JsonModel;
import com.tomjun.permission.model.UserInfo;
import com.tomjun.permission.service.LoginService;

/**
 * @author: hxy
 * @description: 登录相关Controller
 * @date: 2017/10/24 10:33
 */
@RestController
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private LoginService loginService;
	
	/**
	 * 登录
	 */
	@PostMapping("/auth")
	public JsonModel<String> authLogin(@RequestBody UserInfo userInfo) {
		JsonModel<String> jsonModel=new JsonModel<String>();
		try {
			loginService.authLogin(userInfo);
			return jsonModel.generateSuccessObject(null);
		} catch (Exception e) {
			// TODO: handle exception
			jsonModel.setCode(1);
			jsonModel.setMsg("账号密码错误");
			return jsonModel;
		}
	}

	/**
	 * 查询当前登录用户的信息
	 */
	@PostMapping("/getInfo")
	public JsonModel<Object> getInfo() {
		JsonModel<Object> infoJsonModel=new JsonModel<Object>();
		try {
			return loginService.getInfo();
		} catch (Exception e) {
			// TODO: handle exception
			infoJsonModel.setCode(1);
			infoJsonModel.setMsg("fail");
			return infoJsonModel;
		}
	}

	/**
	 * 登出
	 */
	@PostMapping("/logout")
	public JsonModel<String> logout() {
		JsonModel<String> jsonModel=new JsonModel<String>();
		try {
			loginService.logout();
			return jsonModel.generateSuccessObject(null);
		} catch (Exception e) {
			// TODO: handle exception
			return jsonModel.generateFailue("fail");
		}
	}
}
